//
//  GLCollectionViewCellForFind.m
//  ttpod
//
//  Created by apple on 16/3/21.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "GLCollectionViewCellForFind.h"
#import "Header.h"

#import "GLTableViewCellForScroll.h"
#import "GLTableViewCellForButton.h"
#import "GLTableViewCellForHot.h"
#import "GLTableViewCellForAlways.h"
#import "GLTableViewCellForSinger.h"
#import "GLTableViewCellForNew.h"
#import "GLTableViewCellForUnique.h"
#import "GLTableViewCellForAsia.h"
#import "YPTableViewCellForMV.h"
#import "YPTableViewCellForEveryone.h"

#import "NetworkHander.h"
#import "YPModelForFound.h"
#import <UIImageView+WebCache.h>

@interface GLCollectionViewCellForFind () <UITableViewDataSource, UITableViewDelegate, NetworkHandlerDelegate>

@property (nonatomic, retain) UITableView *tableViewForFind;
@property (nonatomic, retain) NSMutableArray *arrForData;
@property (nonatomic, retain) UIView *viewForTableViewHead;

@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)NSMutableArray *arrForPicURL;
@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, retain)UIPageControl *page;
@property (nonatomic, retain)UIImageView *imageView1;

@end

@implementation GLCollectionViewCellForFind

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createTableView];
        [self handleData];
        
    }
    
    return self;
    
}

- (void)handleData {
    
    NSString *str = @"http://api.dongting.com/frontpage/frontpage";
    
    [NetworkHander handlerJSONWithURL:str delegate:self];
}

- (void)handlerDidComplete:(id)result {
    
    self.arrForData = [NSMutableArray array];
    NSArray *arrTemp = [result objectForKey:@"data"];
    
    for (NSDictionary *dic in arrTemp) {
        
        YPModelForFound *model = [[YPModelForFound alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.arrForData addObject:model];
    }
    
    NSLog(@"%@", self.arrForData);
    [self createScrollView];
    [self createPage];
    [self createTimer];
    [self.tableViewForFind reloadData];
}

#pragma mark - 创建tableView
- (void)createTableView {
    
    self.tableViewForFind = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
    
    self.viewForTableViewHead = [[UIView alloc] initWithFrame:CGRectMake(0, 64, self.frame.size.width, self.frame.size.width / 2)];

    self.tableViewForFind.tableHeaderView = self.viewForTableViewHead;
    [self.contentView addSubview:self.tableViewForFind];
    
    self.tableViewForFind.dataSource = self;
    self.tableViewForFind.delegate = self;
    
    self.tableViewForFind.backgroundColor = [UIColor whiteColor];
    
    self.tableViewForFind.separatorStyle = UITableViewCellSelectionStyleNone;
    
#pragma mark - 频道入口
    [self.tableViewForFind registerClass:[GLTableViewCellForButton class] forCellReuseIdentifier:@"poolForTableViewCellOfButton"];
    
#pragma mark - 大家在听
    [self.tableViewForFind registerClass:[YPTableViewCellForEveryone class] forCellReuseIdentifier:@"poolForTableViewCellOfEveryone"];
    
#pragma mark - 热门歌单
    [self.tableViewForFind registerClass:[GLTableViewCellForHot class] forCellReuseIdentifier:@"poolForTableViewCellOfHot"];
    
#pragma mark - 总有
    [self.tableViewForFind registerClass:[GLTableViewCellForAlways class] forCellReuseIdentifier:@"poolForTableViewCellOfAlways"];
    
#pragma mark - 我是歌手
    [self.tableViewForFind registerClass:[GLTableViewCellForSinger class] forCellReuseIdentifier:@"poolForTableViewCellOfSinger"];
    
#pragma mark - 新歌新碟
    [self.tableViewForFind registerClass:[GLTableViewCellForNew class] forCellReuseIdentifier:@"poolForTableViewCellOfNew"];
    
#pragma mark - MV
    [self.tableViewForFind registerClass:[YPTableViewCellForMV class] forCellReuseIdentifier:@"poolForTableViewCellOfMV"];
    
#pragma mark - 独家
    [self.tableViewForFind registerClass:[GLTableViewCellForUnique class] forCellReuseIdentifier:@"poolForTableViewCellOfUnique"];
    
#pragma mark - 亚洲
    [self.tableViewForFind registerClass:[GLTableViewCellForAsia class] forCellReuseIdentifier:@"poolForTableViewCellOfAsia"];
    
    [self.tableViewForFind registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    

    if (section == 0) {
        
        return 1;
    }else if (section == 1 || section == 4 || section == 6) {

        return 3;
    }else {
        return 1;
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.arrForData.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    YPModelForFound *model = [self.arrForData objectAtIndex:indexPath.section + 1];
    
    if (indexPath.section == 0) {
        
        GLTableViewCellForButton *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForTableViewCellOfButton"];
        
        [cell passModelForFoundButtonTal:model];
        return cell;
        
    }else if (indexPath.section == 1) {
        
        YPTableViewCellForEveryone *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForTableViewCellOfEveryone"];
        
        
        return cell;
        
    }else {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return 60;

    }else if (indexPath.section == 1){
        
        return 60;
    }else {
        
        return 100;
    }
}

// 自定义区头.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 2 || section == 3 || section == 5 || section == 6 || section == 7 ) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CWIDTH, 60)];
        
        UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, CWIDTH / 3 * 2, 40)];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [view addSubview:labelTitle];
        [view addSubview:button];
        
        
        return view;
        
    }else if (section == 4 || section == 8 || section == 9) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CWIDTH, 60)];
        
        UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, CWIDTH / 3 * 2, 25)];

        [view addSubview:labelTitle];
        
        return view;
        
    }else {
        
        return nil;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 0;
    }else {
        
        return 40;

    }
}


#pragma mark - 自动轮播图
- (void)createScrollView {
    
    NSMutableArray *arr = [NSMutableArray array];
    
    YPModelForFound *model = [self.arrForData objectAtIndex:0];
    
    for (NSDictionary *dic in model.data) {
        
        [arr addObject:[dic valueForKey:@"picUrl"]];
    }
    [arr addObject:arr.firstObject];
    [arr insertObject:[arr objectAtIndex:arr.count - 2]  atIndex:0];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.viewForTableViewHead.frame.size.width, self.viewForTableViewHead.frame.size.width / 2)];
    self.scrollView.pagingEnabled = YES;
    [self.viewForTableViewHead addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(arr.count * self.viewForTableViewHead.frame.size.width, 0);
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    for (int i = 0; i < arr.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView sd_setImageWithURL:[NSURL URLWithString:arr[i]]
                     placeholderImage:[UIImage imageNamed:@"loading"]];
        
        imageView.userInteractionEnabled = YES; // UIImageView 的用户交互一定要打开，因为UIImageView默认是关闭的，如果不打开，将无法响应手势.
        
        imageView.frame = CGRectMake(self.viewForTableViewHead.frame.size.width * i, 0, self.viewForTableViewHead.frame.size.width, self.viewForTableViewHead.frame.size.width / 2);
        [self.scrollView addSubview:imageView];
        
        imageView.tag = i;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAct:)];
        
        [imageView addGestureRecognizer:tap];
    }
    self.scrollView.contentOffset = CGPointMake(self.viewForTableViewHead.frame.size.width, 0);
    self.scrollView.delegate = self;
}


- (void)tapAct:(UITapGestureRecognizer *)tap {
    
    NSInteger i = [tap view].tag;

    NSLog(@"%ld", i);
}


- (void)createTimer {
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}

- (void)removeTimer {
    
    [self.timer invalidate];
}

- (void)nextImage {
    
    if (self.scrollView.contentOffset.x == self.viewForTableViewHead.frame.size.width * 5) {
        
        self.page.currentPage = 0;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            int contentOfset = (int)self.scrollView.contentOffset.x;
            self.scrollView.contentOffset = CGPointMake(contentOfset + self.viewForTableViewHead.frame.size.width, 0);
        }];
        
    }else {
        
        if (self.scrollView.contentOffset.x == 0) {
            self.scrollView.contentOffset = CGPointMake(self.viewForTableViewHead.frame.size.width * 5, 0);
        }
        if (self.scrollView.contentOffset.x == self.viewForTableViewHead.frame.size.width * 6) {
            self.scrollView.contentOffset = CGPointMake(self.viewForTableViewHead.frame.size.width, 0);
        }
        
        self.page.currentPage = self.scrollView.contentOffset.x / self.viewForTableViewHead.frame.size.width;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            int contentOfset = (int)self.scrollView.contentOffset.x;
            
            self.scrollView.contentOffset = CGPointMake(contentOfset + self.viewForTableViewHead.frame.size.width, 0);
        }];
    }
}

- (void)createPage {
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, self.viewForTableViewHead.frame.size.width / 4, self.viewForTableViewHead.frame.size.height / 10)];
    self.page.center = CGPointMake(self.viewForTableViewHead.frame.size.width / 2, self.viewForTableViewHead.frame.size.height / 10 * 9);
    
    [self.viewForTableViewHead addSubview:self.page];

    self.page.backgroundColor = [UIColor clearColor];
    self.page.numberOfPages = 5;
    
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)pageAction:(UIPageControl *)page {
    
    [self.scrollView setContentOffset:CGPointMake(page.currentPage * self.viewForTableViewHead.frame.size.width, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (self.scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(self.viewForTableViewHead.frame.size.width * 5, 0);
    }
    if (self.scrollView.contentOffset.x == self.viewForTableViewHead.frame.size.width * 6) {
        self.scrollView.contentOffset = CGPointMake(self.viewForTableViewHead.frame.size.width, 0);
    }
    self.page.currentPage = self.scrollView.contentOffset.x / self.viewForTableViewHead.frame.size.width - 1;
}

// 停止触摸轮播图时重新加入定时器.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    //    NSLog(@"重新加入定时器");
    [self createTimer];
}

// 开始触摸轮播图时移除定时器.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    //    NSLog(@"移除定时器");
    [self removeTimer];
}


@end
